﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Web;
using System.Globalization;
using System.Configuration;
using Database;
using ConnectDatabase;
using IBE.LccWS;

namespace IBE
{
    public class LowcostParser
    {
        #region Get data to caching
        public struct GeoData
        {
            public string Code { get; set; }
            public tblGeoAirport Airport { get; set; }
            public tblGeoCity City { get; set; }
        };

        public List<GeoData> GetListGeoData(List<FlightFare> departureFlightFare, List<FlightFare> returnFlightFare)
        {
            List<string> listAirportCode = new List<string>();
            foreach (FlightFare e in departureFlightFare)
            {
                listAirportCode.Add(e.StartPoint);
                listAirportCode.Add(e.EndPoint);
                foreach (AvailFlight item in e.ListAvailFlights)
                {
                    listAirportCode.Add(item.StartPoint);
                    listAirportCode.Add(item.EndPoint);
                }
            }

            if (returnFlightFare != null)
            {
                foreach (FlightFare e in returnFlightFare)
                {
                    listAirportCode.Add(e.StartPoint);
                    listAirportCode.Add(e.EndPoint);
                    foreach (AvailFlight item in e.ListAvailFlights)
                    {
                        listAirportCode.Add(item.StartPoint);
                        listAirportCode.Add(item.EndPoint);
                    }
                }
            }

            var items = listAirportCode.GroupBy(item => item).Select(g => new { key = g.Key }).ToList();

            List<GeoData> listGeoData = new List<GeoData>();
            foreach (var code in items)
            {
                GeoData item = new GeoData();
                item.Code = code.key;
                tblGeoAirport obj = new GeoAirportDB().GetByKey(code.key);
                if (obj != null)
                {
                    item.Airport = obj;
                    item.City = obj.tblGeoCity;
                    listGeoData.Add(item);
                }
            }
            return listGeoData;
        }

        public List<tblAirline> GetListAirlineDetail()
        {
            List<tblAirline> listAirlineDetail = new List<tblAirline>();
            listAirlineDetail.Add(new AirlinesDB().GetByKey("VN"));
            listAirlineDetail.Add(new AirlinesDB().GetByKey("JQ"));
            listAirlineDetail.Add(new AirlinesDB().GetByKey("VJ"));
            listAirlineDetail.Add(new AirlinesDB().GetByKey("BL"));
            listAirlineDetail.Add(new AirlinesDB().GetByKey("AK"));
            listAirlineDetail.Add(new AirlinesDB().GetByKey("TR"));
            return listAirlineDetail;
        }

        protected string ConvertClass(string originClass)
        {
            List<string> list = new List<string>();
            list.Add("J");
            list.Add("C");
            list.Add("D");
            list.Add("I");
            list.Add("Z");
            list.Add("SkyBoss");
            list.Add("Business");

            string businessClass = list.SingleOrDefault(a => a.ToLower() == originClass.ToLower());
            if (string.IsNullOrEmpty(businessClass))
            {
                return "Economic";
            }
            else
            {
                return "Business";
            }
        }
        #endregion

        #region Parsing data
        public List<FareData> GetLowcostFareData(List<FlightData> listFlightsRepository)
        {
            List<FareData> listData = new List<FareData>();
            try
            {
                List<tblAirline> listAirlineDetail = GetListAirlineDetail();

                int fareDataIdContinue = 0;
                foreach (FlightData item in listFlightsRepository)
                {
                    List<List<FlightFare>> groupDepartureFlightFare = new List<List<FlightFare>>();
                    List<List<FlightFare>> groupReturnFlightFare = new List<List<FlightFare>>();
                    groupDepartureFlightFare = item.DepartureFlights.GroupBy(u => u.TotalPrice).Select(grp => grp.ToList()).ToList();
                    if (item.ReturnFlights != null && item.ReturnFlights.Count() > 0)
                    {
                        groupReturnFlightFare = item.ReturnFlights.GroupBy(u => u.TotalPrice).Select(grp => grp.ToList()).ToList();
                    }
                    List<GeoData> listGeoData = GetListGeoData(item.DepartureFlights.ToList(), item.ReturnFlights != null ? item.ReturnFlights.ToList() : null);
                    List<FareData> tempList = ParseFareDataFromFlightFare(groupDepartureFlightFare, groupReturnFlightFare, listAirlineDetail, listGeoData, item.SessionAll, item.AirlineCode, fareDataIdContinue);
                    foreach (FareData flight in tempList)
                    {
                        listData.Add(flight);
                    }
                    if (listData.Count > 0)
                    {
                        fareDataIdContinue = listData[listData.Count - 1].FareDataId + 1;
                    }
                }
            }
            catch
            {
                return listData;
            }

            return listData;
        }

        public List<FareData> ParseFareDataFromFlightFare(List<List<FlightFare>> groupDepartureFlightFare, List<List<FlightFare>> groupReturnFlightFare, List<tblAirline> listAirlineDetail, List<GeoData> listGeoData, string sessionId, string airlineCode, int fareDataIdContinue)
        {
            IBE.Common utility = new IBE.Common();
            List<FareData> listData = new List<FareData>();
            string currency = ConfigurationManager.AppSettings["default_currency"].ToString();

            int fareDataId = fareDataIdContinue;
            foreach (List<FlightFare> listDepartureFlightFare in groupDepartureFlightFare)
            {
                if (groupReturnFlightFare.Count == 0)
                {
                    #region One way
                    FareData fareData = new FareData();
                    fareData.TotalAdult = Convert.ToInt32(listDepartureFlightFare[0].Adult);
                    fareData.TotalChild = Convert.ToInt32(listDepartureFlightFare[0].Child);
                    fareData.TotalInfant = Convert.ToInt32(listDepartureFlightFare[0].Infant);
                    fareData.TotalPax = fareData.TotalAdult + fareData.TotalChild + fareData.TotalInfant;
                    fareData.FareAdult = Common.MoneyExchange(listDepartureFlightFare[0].PriceAdult + listDepartureFlightFare[0].FeeAdult + listDepartureFlightFare[0].TaxAdult, listDepartureFlightFare[0].Currency, currency);
                    fareData.FareChild = Common.MoneyExchange(listDepartureFlightFare[0].PriceChild + listDepartureFlightFare[0].FeeChild + listDepartureFlightFare[0].TaxChild, listDepartureFlightFare[0].Currency, currency);
                    fareData.FareInfant = Common.MoneyExchange(listDepartureFlightFare[0].PriceInfant + listDepartureFlightFare[0].FeeInfant + listDepartureFlightFare[0].TaxInfant, listDepartureFlightFare[0].Currency, currency);
                    fareData.TotalFare = Common.MoneyExchange(listDepartureFlightFare[0].TotalPrice, listDepartureFlightFare[0].Currency, currency);
                    fareData.TotalFareRaw = listDepartureFlightFare[0].TotalPrice;
                    fareData.PlatingCarrier = airlineCode;
                    fareData.CurrencyCode = currency;

                    string continentCode = listGeoData.FirstOrDefault(a => a.Code == listDepartureFlightFare[0].EndPoint).Airport.tblGeoCity.tblGeoCountry.tblGeoContinent.Code;
                    tblFee adtFee = new FeeDB().SearchFee(1, airlineCode, "LCC", continentCode, "ADT");
                    if (adtFee != null)
                    {
                        fareData.FeeAdult = adtFee.IsAmount.Value ? adtFee.Fee.Value : adtFee.Fee.Value * fareData.FareAdult / 100;
                        fareData.FeeAdult = Common.MoneyExchange(fareData.FeeAdult, listDepartureFlightFare[0].Currency, currency);
                    }
                    else
                    {
                        fareData.FeeAdult = ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_adt_ow"].ToString()) * fareData.FareAdult / 100;
                    }

                    if (fareData.TotalChild > 0)
                    {
                        tblFee chdFee = new FeeDB().SearchFee(1, airlineCode, "LCC", continentCode, "CHD");
                        if (chdFee != null)
                        {
                            fareData.FeeChild = chdFee.IsAmount.Value ? chdFee.Fee.Value : chdFee.Fee.Value * fareData.FareChild / 100;
                            fareData.FeeChild = Common.MoneyExchange(fareData.FeeChild, listDepartureFlightFare[0].Currency, currency);
                        }
                        else
                        {
                            fareData.FeeChild = ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_chd_ow"].ToString()) * fareData.FareChild / 100;
                        }
                    }


                    if (fareData.TotalInfant > 0)
                    {
                        tblFee infFee = new FeeDB().SearchFee(1, airlineCode, "LCC", continentCode, "INF");
                        if (infFee != null)
                        {
                            fareData.FeeInfant = infFee.IsAmount.Value ? infFee.Fee.Value : infFee.Fee.Value * fareData.FareInfant / 100;
                            fareData.FeeInfant = Common.MoneyExchange(fareData.FeeInfant, listDepartureFlightFare[0].Currency, currency);
                        }
                        else
                        {
                            fareData.FeeInfant = ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_inf_ow"].ToString()) * fareData.FareInfant / 100;
                        }
                    }

                    fareData.TotalFee = fareData.TotalAdult * fareData.FeeAdult + fareData.TotalChild * fareData.FeeChild + fareData.TotalInfant * fareData.FeeInfant;

                    fareData.IBE = "lowcost";
                    fareData.SessionId = sessionId;

                    fareData.DepartureDate = listDepartureFlightFare[0].StartDate;
                    if (groupReturnFlightFare.Count > 0)
                    {
                        fareData.ReturnDate = groupReturnFlightFare[0][0].StartDate;
                    }

                    //fareData.TotalDuration
                    fareData.FareDataId = fareDataId;

                    #region Departure flights
                    int index = 0;
                    fareData.ListDepartureFlight = new List<Flight>();
                    foreach (FlightFare flightFare in listDepartureFlightFare)
                    {
                        Flight flight = new Flight();

                        flight.StartPointAirport = listGeoData.FirstOrDefault(a => a.Code == flightFare.StartPoint).Airport;
                        flight.EndPointAirport = listGeoData.FirstOrDefault(a => a.Code == flightFare.EndPoint).Airport;
                        flight.StartPointCity = listGeoData.FirstOrDefault(a => a.Code == flightFare.StartPoint).City;
                        flight.EndPointCity = listGeoData.FirstOrDefault(a => a.Code == flightFare.EndPoint).City;
                        flight.AirlineDetail = listAirlineDetail.FirstOrDefault(a => a.Code == airlineCode);
                        flight.AirlineLogo = flight.AirlineDetail.Logo;
                        flight.StartTm = flightFare.StartDate;
                        flight.EndTm = flightFare.EndDate;
                        flight.Duration = flight.EndTm - flight.StartTm;

                        flight.ListAvailFlt = GetAvailFltFromAvailFare(flightFare.ListAvailFlights.ToList(), flight, listGeoData);

                        flight.Index = index;
                        flight.FareDataId = fareData.FareDataId;
                        flight.Checked = index == 0 ? "checked" : "";
                        flight.StopNum = flightFare.ListAvailFlights.Count() - 1;
                        flight.HasDownStop = "0";
                        flight.NoRefund = false;
                        flight.SelectValue = flightFare.SelectValue;
                        flight.FlightInfo = "";

                        index++;
                        fareData.ListDepartureFlight.Add(flight);
                    }
                    #endregion

                    listData.Add(fareData);
                    fareDataId++;
                    #endregion
                }
                else
                {
                    #region Round trip
                    foreach (List<FlightFare> listReturnFlightFare in groupReturnFlightFare)
                    {
                        FareData fareData = new FareData();
                        fareData.HasReturn = true;
                        fareData.TotalAdult = Convert.ToInt32(listDepartureFlightFare[0].Adult);
                        fareData.TotalChild = Convert.ToInt32(listDepartureFlightFare[0].Child);
                        fareData.TotalInfant = Convert.ToInt32(listDepartureFlightFare[0].Infant);
                        fareData.TotalPax = fareData.TotalAdult + fareData.TotalChild + fareData.TotalInfant;
                        fareData.FareAdult = Common.MoneyExchange(listDepartureFlightFare[0].PriceAdult + listDepartureFlightFare[0].FeeAdult + listDepartureFlightFare[0].TaxAdult + listReturnFlightFare[0].PriceAdult + listReturnFlightFare[0].FeeAdult + listReturnFlightFare[0].TaxAdult, listDepartureFlightFare[0].Currency, currency);
                        fareData.FareChild = Common.MoneyExchange(listDepartureFlightFare[0].PriceChild + listDepartureFlightFare[0].FeeChild + listDepartureFlightFare[0].TaxChild + listReturnFlightFare[0].PriceChild + listReturnFlightFare[0].FeeChild + listReturnFlightFare[0].TaxChild, listDepartureFlightFare[0].Currency, currency);
                        fareData.FareInfant = Common.MoneyExchange(listDepartureFlightFare[0].PriceInfant + listDepartureFlightFare[0].FeeInfant + listDepartureFlightFare[0].TaxInfant + listReturnFlightFare[0].PriceInfant + listReturnFlightFare[0].FeeInfant + listReturnFlightFare[0].TaxInfant, listDepartureFlightFare[0].Currency, currency);
                        fareData.TotalFare = Common.MoneyExchange(listDepartureFlightFare[0].TotalPrice + listReturnFlightFare[0].TotalPrice, listDepartureFlightFare[0].Currency, currency);
                        fareData.TotalFareRaw = listDepartureFlightFare[0].TotalPrice + listReturnFlightFare[0].TotalPrice;
                        fareData.PlatingCarrier = airlineCode;

                        fareData.CurrencyCode = currency;

                        string continentCode = listGeoData.FirstOrDefault(a => a.Code == listDepartureFlightFare[0].EndPoint).Airport.tblGeoCity.tblGeoCountry.tblGeoContinent.Code;
                        tblFee adtFee = new FeeDB().SearchFee(2, airlineCode, "LCC", continentCode, "ADT");
                        if (adtFee != null)
                        {
                            fareData.FeeAdult = adtFee.IsAmount.Value ? adtFee.Fee.Value : adtFee.Fee.Value * fareData.FareAdult / 100;
                            fareData.FeeAdult = Common.MoneyExchange(fareData.FeeAdult, listDepartureFlightFare[0].Currency, currency);
                        }
                        else
                        {
                            fareData.FeeAdult = ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_adt_rt"].ToString()) * fareData.FareAdult / 100;
                        }

                        if (fareData.TotalChild > 0)
                        {
                            tblFee chdFee = new FeeDB().SearchFee(2, airlineCode, "LCC", continentCode, "CHD");
                            if (chdFee != null)
                            {
                                fareData.FeeChild = chdFee.IsAmount.Value ? chdFee.Fee.Value : chdFee.Fee.Value * fareData.FareChild / 100;
                                fareData.FeeChild = Common.MoneyExchange(fareData.FeeChild, listDepartureFlightFare[0].Currency, currency);
                            }
                            else
                            {
                                fareData.FeeChild = ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_chd_rt"].ToString()) * fareData.FareChild / 100;
                            }
                        }

                        if (fareData.TotalInfant > 0)
                        {
                            tblFee infFee = new FeeDB().SearchFee(2, airlineCode, "LCC", continentCode, "INF");
                            if (infFee != null)
                            {
                                fareData.FeeInfant = infFee.IsAmount.Value ? infFee.Fee.Value : infFee.Fee.Value * fareData.FareInfant / 100;
                                fareData.FeeInfant = Common.MoneyExchange(fareData.FeeInfant, listDepartureFlightFare[0].Currency, currency);
                            }
                            else
                            {
                                fareData.FeeInfant = ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_inf_rt"].ToString()) * fareData.FareInfant / 100;
                            }
                        }

                        fareData.TotalFee = fareData.TotalAdult * fareData.FeeAdult + fareData.TotalChild * fareData.FeeChild + fareData.TotalInfant * fareData.FeeInfant;

                        fareData.IBE = "lowcost";
                        fareData.SessionId = sessionId;

                        fareData.DepartureDate = listDepartureFlightFare[0].StartDate;
                        if (groupReturnFlightFare.Count > 0)
                        {
                            fareData.ReturnDate = listReturnFlightFare[0].StartDate;
                        }

                        //fareData.TotalDuration
                        fareData.FareDataId = fareDataId;

                        #region Departure flights
                        int flightIndex = 0;
                        fareData.ListDepartureFlight = new List<Flight>();
                        foreach (FlightFare flightFare in listDepartureFlightFare)
                        {
                            Flight flight = new Flight();

                            flight.StartPointAirport = listGeoData.FirstOrDefault(a => a.Code == flightFare.StartPoint).Airport;
                            flight.EndPointAirport = listGeoData.FirstOrDefault(a => a.Code == flightFare.EndPoint).Airport;

                            flight.StartPointCity = listGeoData.FirstOrDefault(a => a.Code == flightFare.StartPoint).City;
                            flight.EndPointCity = listGeoData.FirstOrDefault(a => a.Code == flightFare.EndPoint).City;

                            flight.AirlineDetail = listAirlineDetail.FirstOrDefault(a => a.Code == airlineCode);
                            flight.AirlineLogo = flight.AirlineDetail.Logo;

                            flight.StartTm = flightFare.StartDate;
                            flight.EndTm = flightFare.EndDate;
                            flight.Duration = flight.EndTm - flight.StartTm;

                            flight.ListAvailFlt = GetAvailFltFromAvailFare(flightFare.ListAvailFlights.ToList(), flight, listGeoData);

                            flight.Index = flightIndex;
                            flight.FareDataId = fareData.FareDataId;
                            flight.Checked = flightIndex == 0 ? "checked" : "";
                            flight.StopNum = flightFare.ListAvailFlights.Count() - 1;
                            flight.HasDownStop = "0";
                            flight.NoRefund = false;
                            flight.SelectValue = flightFare.SelectValue;
                            flight.FlightInfo = "";

                            flightIndex++;
                            fareData.ListDepartureFlight.Add(flight);
                        }
                        #endregion

                        #region Return flight
                        flightIndex = 0;
                        fareData.ListReturnFlight = new List<Flight>();
                        foreach (FlightFare flightFare in listReturnFlightFare)
                        {
                            Flight flight = new Flight();

                            flight.StartPointAirport = listGeoData.FirstOrDefault(a => a.Code == flightFare.StartPoint).Airport;
                            flight.EndPointAirport = listGeoData.FirstOrDefault(a => a.Code == flightFare.EndPoint).Airport;

                            flight.StartPointCity = listGeoData.FirstOrDefault(a => a.Code == flightFare.StartPoint).City;
                            flight.EndPointCity = listGeoData.FirstOrDefault(a => a.Code == flightFare.EndPoint).City;

                            flight.AirlineDetail = listAirlineDetail.FirstOrDefault(a => a.Code == airlineCode);
                            flight.AirlineLogo = flight.AirlineDetail.Logo;

                            flight.StartTm = flightFare.StartDate;
                            flight.EndTm = flightFare.EndDate;
                            flight.Duration = flight.EndTm - flight.StartTm;

                            flight.ListAvailFlt = GetAvailFltFromAvailFare(flightFare.ListAvailFlights.ToList(), flight, listGeoData);

                            flight.Index = flightIndex;
                            flight.FareDataId = fareData.FareDataId;
                            flight.Checked = flightIndex == 0 ? "checked" : "";
                            flight.StopNum = flightFare.ListAvailFlights.Count() - 1;
                            flight.HasDownStop = "0";
                            flight.NoRefund = false;
                            flight.SelectValue = flightFare.SelectValue;
                            flight.FlightInfo = "";

                            flightIndex++;
                            fareData.ListReturnFlight.Add(flight);
                        }
                        #endregion

                        if (fareData.ListReturnFlight.Count > 0)
                        {
                            listData.Add(fareData);
                        }
                        else
                        {
                            continue;
                        }
                        fareDataId++;
                    }
                    #endregion
                }
            }

            return listData;
        }

        public List<AvailFlt> GetAvailFltFromAvailFare(List<AvailFlight> listAvailFare, Flight flight, List<GeoData> listGeoData)
        {
            IBE.Common utility = new IBE.Common();
            List<AvailFlt> listAvaiFlt = new List<AvailFlt>();

            int count = 0;
            int i = 0;
            foreach (AvailFlight item in listAvailFare)
            {
                AvailFlt avaiFlt = new AvailFlt();

                DateTime startTime = item.StartDate;
                DateTime endTime = item.EndDate;

                avaiFlt.AirlineDetail = flight.AirlineDetail;
                avaiFlt.AirpChg = "";
                avaiFlt.AirV = flight.AirlineDetail.Code;
                avaiFlt.ChangeAirport = "";
                avaiFlt.ChangeStation = "";
                avaiFlt.ClassAdult = item.Class;
                avaiFlt.ClassChild = item.Class;
                avaiFlt.ClassInfant = item.Class;
                avaiFlt.Conx = "";
                avaiFlt.DayChg = "";
                avaiFlt.DaysOperates = "";
                avaiFlt.DispOption = "";
                avaiFlt.EndAirp = item.EndPoint;
                avaiFlt.EndDt = endTime.ToString("yyyyMMdd");
                avaiFlt.EndPointAirport = listGeoData.FirstOrDefault(a => a.Code == item.EndPoint).Airport;
                avaiFlt.EndPointCity = listGeoData.FirstOrDefault(a => a.Code == item.EndPoint).City;
                //avaiFlt.EndPointCountry
                avaiFlt.EndTerminal = "";
                avaiFlt.EndTm = endTime.ToString("HHmm");
                avaiFlt.Equip = "";
                avaiFlt.ETktEligibility = "";
                avaiFlt.FltNum = item.FlightNumber;
                avaiFlt.FltTm = (endTime - startTime).TotalMinutes.ToString();
                avaiFlt.FrstDwnlnStp = "";
                //avaiFlt.avaiFlt.FrstDwnlnStpCity
                avaiFlt.GalileoAirVInd = "";
                avaiFlt.GenTrafRestriction = "";
                avaiFlt.InsideAvailOption = "";

                if (count == listAvailFare.Count - 1)
                {
                    avaiFlt.IsLastItem = "1";
                }
                else
                {
                    avaiFlt.IsLastItem = "0";
                }


                avaiFlt.JrnyTm = (endTime - startTime).TotalMinutes.ToString();
                avaiFlt.LastDwnlnStp = "";
                //avaiFlt.LastDwnlnStpCity
                avaiFlt.LinkSellAgrmnt = "";
                //avaiFlt.ListPax
                //avaiFlt.ListPaxType
                avaiFlt.LSAInd = "";
                avaiFlt.NeutralFltLineNum = "";
                avaiFlt.NonRefund = false;
                avaiFlt.NumStops = (listAvailFare.Count - 1).ToString();
                avaiFlt.OpAirlineDetail = flight.AirlineDetail;
                avaiFlt.OpAirV = flight.AirlineDetail.Code;
                avaiFlt.OpAirVInd = flight.AirlineDetail.Code;
                avaiFlt.OpFltDesignator = "";
                avaiFlt.OpFltSuf = "";
                avaiFlt.OpSuf = "";
                avaiFlt.Perf = "";
                avaiFlt.ScheduleLevelCarrier = "";
                avaiFlt.Spare1 = "";
                avaiFlt.SponsoredFltInd = "";
                avaiFlt.SponsoredFltKey = "";
                avaiFlt.SponsoredFltLineNum = "";
                avaiFlt.StartAirp = item.StartPoint;
                avaiFlt.StartDt = startTime.ToString("yyyyMMdd");
                avaiFlt.StartPointAirport = listGeoData.FirstOrDefault(a => a.Code == item.StartPoint).Airport;
                avaiFlt.StartPointCity = listGeoData.FirstOrDefault(a => a.Code == item.StartPoint).City;
                //avaiFlt.StartPointCountry
                avaiFlt.StartTerminal = "";
                avaiFlt.StartTm = startTime.ToString("HHmm");
                //avaiFlt.StopAirport
                //avaiFlt.StopCity
                avaiFlt.StopOvernight = "";
                avaiFlt.IBE = "lowcost";
                avaiFlt.Class = ConvertClass(item.Class);

                if (count < listAvailFare.Count - 1)
                {
                    avaiFlt.StopTime = listAvailFare[i + 1].StartDate - endTime;
                }

                i++;
                count++;
                listAvaiFlt.Add(avaiFlt);
            }

            return listAvaiFlt;
        }
        #endregion

        #region Utilities
        public double ConvertStringToDouble(string value)
        {
            CultureInfo culture = CultureInfo.CreateSpecificCulture("es-ES");
            try
            {
                return double.Parse(value, culture);
            }
            catch { return 0; }
        }

        private double StringToDouble(string input)
        {
            input = string.IsNullOrEmpty(input) ? "0" : input;
            try
            {
                return double.Parse(input, CultureInfo.CreateSpecificCulture("es-ES"));
            }
            catch (Exception) { return 0; }
        }
        #endregion

        #region Crawler
        public List<FlightData> SearchLccFlight(int itineraryType, string startPoint, string endPoint, DateTime departureDate, DateTime returnDate, int adt, int chd, int inf, List<LccInfo> listLccInfo)
        {
            try
            {
                List<FlightData> data = new List<FlightData>();

                LccWS.LccService soap = new LccWS.LccService();
                soap.AuthenticationValue = new LccWS.Authentication { Username = "nqminh", Password = "NQMinh@120386" };
                data = soap.FlightSearch(itineraryType, startPoint, endPoint, departureDate, returnDate, adt, chd, inf, listLccInfo.ToArray(), true).ToList();

                return data;
            }
            catch
            {
                return new List<FlightData>();
            }
        }

        public BaggageInfo GetBaggages(string airline, int itineraryType, string departSelectValue, string returnSelectValue, string session, bool isDomestic, int segmentDepart, int segmentReturn)
        {
            LccWS.LccService soap = new LccWS.LccService();
            soap.AuthenticationValue = new LccWS.Authentication { Username = "nqminh", Password = "NQMinh@120386" };
            return soap.GetBaggages(airline, itineraryType, departSelectValue, returnSelectValue, session, isDomestic, segmentDepart, segmentReturn);
        }

        public string SubmitBooking(string airline, int itineraryType, string startPoint, string endPoint, DateTime departDate, DateTime returnDate, int adt, int chd, int inf, string departSelectValue, string returnSelectValue, double totalPriceDepart, double totalPriceReturn, List<Passenger> listPax, Contact contact)
        {
            string bookingCode = "";
            string account = "";
            string password = "";
            if (airline == "JQ")
            {
                account = "";
                password = "";
            }
            else if (airline == "VJ")
            {
                account = "";
                password = "";
            }

            LccWS.LccService soap = new LccWS.LccService();
            soap.AuthenticationValue = new LccWS.Authentication { Username = "nqminh", Password = "NQMinh@120386" };
            bookingCode = soap.SubmitBooking(airline, account, password, itineraryType, startPoint, endPoint, departDate, returnDate, adt, chd, inf, departSelectValue, returnSelectValue, totalPriceDepart, totalPriceReturn, listPax.ToArray(), contact);
           
            return bookingCode;
        }

        public FlightFare GetTotalPriceVN(int itineraryType, string departSelectValue, string returnSelectValue, string session)
        {
            LccWS.LccService soap = new LccWS.LccService();
            soap.AuthenticationValue = new LccWS.Authentication { Username = "nqminh", Password = "NQMinh@120386" };
            return soap.GetTotalPriceVN(itineraryType, departSelectValue, returnSelectValue, session);
        }
        #endregion
    }
}
